#ifndef _COMM_HANDLER_H
#define _COMM_HANDLER_H

DLL_EXPORT extern ConfigFile* world_config;
class LogonClientHandler;

class DLL_EXPORT LogonClientWorker : public CThread
{
	friend class LogonClientHandler;
	Mutex m_readlock;
	vector<RealmPacket*> m_readbuffer;
	void Loop();
};

class DLL_EXPORT LogonClientHandler : public CThread, public Single<LogonClientHandler>
{
	friend class LogonClientWorker;
	SOCKET m_listensocket;
	char* m_realmname, *m_realmaddr;
	void SendPacket(RealmPacket& pack);
	void OnConnect();
	ui8 m_hash[16];
	ui16 m_port;
	ui8 icon, color;
	float population;
public:
	LogonClientHandler() 
	{
		m_realmname = world_config->GetChar("RealmInfo", "Name");
		m_realmaddr = world_config->GetChar("RealmInfo", "Addr");
		char* iconstring = world_config->GetChar("RealmInfo", "Icon");
		char* colorstring = world_config->GetChar("RealmInfo", "Color");
		if(!iconstring)
			icon = 0;
		else
		{
			if(!strcmp(iconstring, "PvP"))
				icon = 3;
			else if(!strcmp(iconstring, "RP"))
				icon = 6;
			else if(!strcmp(iconstring, "RPPvP"))
				icon = 8;
			else
				icon = 0;
		}
		if(!colorstring)
			color = 32;
		else
		{
			if(!strcmp(colorstring, "Green"))
				color = 3;
			else if(!strcmp(colorstring, "Blue"))
				color = 32;
			else if(!strcmp(colorstring, "Red"))
				color = 2;
			else
				color = 32;
		}
		population = world_config->GetFloat("RealmInfo", "Population", 0.0f);
		m_port = 0;
		for(register ui32 i = 0; i < strlen(m_realmaddr); ++i)
			if(*(m_realmaddr + i) == ':')
				m_port = atoi(m_realmaddr + i + 1);
		MD5Hash hash;
		hash.Initialize();
		ui8* passw = (ui8*)world_config->GetChar("Authentication", "Passwort");
		hash.UpdateData(passw, strlen((char*)passw));
		hash.Finalize();
		memcpy(m_hash, hash.GetDigest(), 16);
	}
	void Loop();
	ui16 GetListenPort() { return m_port; }
	void RequestInformation(char* accname, ui32 requestid);
};

#define sLogonClient LogonClientHandler::getSingle()

#endif